<template>
    <!-- 质量检查情况 -->
    <a-spin :spinning="loading">
      <a-card>
        <title-name :title="`${$route.query.month}月质量风险评价表`"></title-name>
        <!-- 详情列表 -->
        <div style="padding: 0 20px 20px;">
          <div v-if="Object.keys(detailMap).length > 0">
              <div v-for="(item, index) in detailMap" :key="index">
                  <div class="branchName">{{ index }}</div>
                  <div>
                  <Empty v-if="!item.length" description="暂无数据" />
                  <a-table v-else :scroll="{ x: 1500 }" rowKey="id" bordered :pagination="false" :columns="columns" :dataSource="item">
                  </a-table>
                </div>
              </div>
          </div>
          <Empty v-else description="暂无数据" />
        </div>
      </a-card>
       <!-- 编辑质量风险信息 -->
    <DialogForm ref="dialogForm" :visible.sync="visible" edit-model="E" headerTitle="编辑质量风险信息" :width="1000" :form="form" :formList="formList" :rules="rules" @confirm="submitForm" />
    </a-spin>
  </template>
  
  <script>
  import { qualityMonthRiskqueryRiskScoreResult, qualityMonthRiskaddOrUpdateMainRisk } from '@/api/quality/qualityMonth/qualityManagementInfo' // api
  import assetApi from '@/api/office/asset' // api
  import DialogForm from '@/components/DialogForm' // 表单弹窗
  import { Empty } from 'ant-design-vue' // 无数据提示
  export default {
    name: 'a' + Date.now(),
    props: {
      // 请求参数
      queryParam: {
        type: Object,
        default: () => null
      }
    },
    components: {
      Empty,
      DialogForm
    },
    data() {
      return {
        loading: false, // 加载控件
        detailMap: {}, // 统计表格列表
        columns: [],
        visible: false,
        rules: {
          mainRisk: [{ required: true, whitespace: true, message: '请输入主要质量风险', trigger: 'change' }]
        }, // 校验规则
        formList: [{ type: 'BaseInput', keyName: 'mainRisk', label: '主要质量风险', span: 16 }], // 弹窗表单配置项
        form: {}
      }
    },
    computed: {
    },
    mounted() {
      // 初始化列表
      this.columns = [
          {
            title: '序号',
            width: 80,
            dataIndex: 'index',
            fixed: 'left',
            align: 'center',
            customRender() {
                return arguments[1].branchComName !== '平均值' ? arguments[2] + 1 : ''
            }
          },
          {
            title: '项目名称',
            width: 200,
            dataIndex: 'projectName'
          },
          {
            title: '综合得分',
            dataIndex: '-1',
            width: 120,
            fixed: 'right',
            align: 'center',
            customRender(e) {
                return e ?? '-'
            }
          }
      ]
      this.getColumns()
    },
    methods: {
      /**
       * 初始化表头
       */
      getColumns() {
        this.loading = true
        this.columns.length === 3 &&
          assetApi.typesLabel({ type: 51 }).then((res) => {
            if (res.code === 200) {
              this.columns.splice(
                2,
                0,
                ...res.data.filter(item => item.code !== '999').map((i) => ({
                  dataIndex: i.code,
                  title: i.name,
                  align: 'center',
                  customRender: (e) => e ?? '-'
                }))
              )
              this.columns.push({
                dataIndex: '999',
                title: '主要质量风险',
                align: 'center',
                width: 200,
                fixed: 'right',
                ellipsis: true,
                customRender: (e) => e ?? '-'
              })
              // 节点若为分公司则添加操作按钮
              this.queryParam.branchComId && (this.$route.query.editModel == 2 || this.$route.query.editModel == 4) &&
                this.columns.push({
                  title: '操作',
                  dataIndex: 'actions',
                  width: 120,
                  align: 'center',
                  fixed: 'right',
                  customRender: (text, record) => {
                    return (
                      <a
                        onClick={() => {
                          this.form.yearMonth = this.queryParam.monthTime
                          this.form.mainRisk = record.mainRisk
                          this.form.qualityMonthId = this.$route.query.id
                          this.form.isAdjust = this.$route.query.isAdjust
                          this.form.projectName = record.projectName
                          this.visible = true
                        }}
                      >
                        编辑
                      </a>
                    )
                  }
                })
            }
            this.init()
          })
      },
      /**
       * 初始化
       */
      init() {
        this.loading = true
        qualityMonthRiskqueryRiskScoreResult({isJurisdiction: false, ...this.queryParam})
        .then((res) => {
          if (res.data) {
            // 映射统计表格信息
            if (res.data.detailMap) {
              this.detailMap = res.data.detailMap || {}
            }
          }
        })
        .finally(() => {
          this.loading = false
        })
      },
      /**
     * 提交表单
     * form：表单
     */
    submitForm(form) {
      this.$refs.dialogForm.loading = true
      qualityMonthRiskaddOrUpdateMainRisk(form)
        .then((res) => {
          if (res.code === '0') {
            this.init()
            this.$message.success('编辑成功！')
            this.visible = false
          }
        })
        .finally(() => {
          this.$refs.dialogForm.loading = false
        })
    }
    }
  }
  </script>
  